﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Commons;
using System.Collections.Generic;
using System.Text;

public partial class OperaEntry_MakerSelect : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DbHelper helper = DbHelper.getHelper();
        StringBuilder sql = new StringBuilder();
        sql.Append("SELECT p.productid, ");
        sql.Append("	p.typeid, ");
        sql.Append("	p.brandid, ");
        sql.Append("    b.brandname,");
        sql.Append("	p.productname, ");
        sql.Append("	p.description, ");
        sql.Append("	p.picture, ");
        sql.Append("	p.price, ");
        sql.Append("	p.onsaledate, ");
        sql.Append("	p.expiredate, ");
        sql.Append("	p.makerid, ");
        sql.Append("    m.makername,");
        sql.Append("	p.onstock, ");
        sql.Append("	p.saled, ");
        sql.Append("	p.regdate");
        sql.Append(" FROM products p");
        sql.Append(" LEFT JOIN makers m");
        sql.Append(" ON p.makerid = m.makerid");
        sql.Append(" LEFT JOIN brands b");
        sql.Append(" ON p.brandid = b.brandid");
        sql.Append(" LEFT JOIN types t");
        sql.Append(" ON t.typeid = p.typeid");
        sql.Append(" where 1 = 1");
        List<string> values = new List<string>();
        if (IsPostBack)
        {
            if (!String.IsNullOrEmpty(this.txtProductId.Text))
            {
                sql.Append(" and productid = @productid");
                values.Add(this.txtProductId.Text);
                
            }
            if (!String.IsNullOrEmpty(this.txtProductName.Text))
            {
                sql.Append(" and productname like '% + @productname + %' ");
                values.Add(this.txtProductId.Text);

            }
        }
        try
        {
            bool isNodata = false;
            DataTable dt = helper.ExcuteDataTableQuery(sql.ToString(), values.ToArray());
            if (dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
                isNodata = true;
            }
            this.grdMaker.DataSource = dt;
            this.grdMaker.DataBind();
            if (isNodata)
            {
                this.grdMaker.Rows[0].Cells.Clear();
                this.grdMaker.Rows[0].Cells.Add(new TableCell());
                this.grdMaker.Rows[0].Cells[0].ColumnSpan = this.grdMaker.Columns.Count;
                this.grdMaker.Rows[0].Cells[0].Text = "没有记录";
                this.grdMaker.Rows[0].Cells[0].Style.Add("text-align", "center");
            }
        }
        catch (Exception ex)
        {
            this.MessageLabel.Text = ex.Message;
        }
    }
    protected void grdMaker_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = "<a href=\"javascript:selectNode('" + ((DataRowView)e.Row.DataItem).Row["productid"] + "','" + ((DataRowView)e.Row.DataItem).Row["productname"] + "');\">选择</a>";
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {

    }
}
